package com.cn.lyl.dao.impl;

import com.cn.lyl.dao.CompanyDao;
import com.cn.lyl.model.CompanyModel;
import com.cn.lyl.util.JDBC;

import java.sql.ResultSet;

public class CompanyDaoImpl implements CompanyDao {
    @Override
    public CompanyModel select() {
        String sql="select phone ,name,mobile,mail,fax,post,location,intro,DATE_FORMAT(update_time,'%Y-%m-%d')as update_time from company where id=1 ";
        CompanyModel companyModel = new CompanyModel();
        ResultSet query = JDBC.query(sql);
        try {
            while (query.next()){
                companyModel.setName(query.getString("name"));
                companyModel.setPhone(query.getString("phone"));
                companyModel.setMobile(query.getString("mobile"));
                companyModel.setMail(query.getString("mail"));
                companyModel.setFax(query.getString("fax"));
                companyModel.setPost(query.getString("post"));
                companyModel.setLocation(query.getString("location"));
                companyModel.setIntro(query.getString("intro"));
                companyModel.setUpdateTime(query.getString("update_time"));
            }
        }catch (Exception e){

        }
        return companyModel;
    }

    @Override
    public int update(CompanyModel companyModel) {
        String sql = "update company set";
        StringBuffer stringBuffer = new StringBuffer();
        if(!"".equals(companyModel.getName())&&companyModel.getName()!=null){
            if (stringBuffer.length() > 0) {
                stringBuffer.append(",");
            }
            stringBuffer.append(" name = '"+companyModel.getName()+"'");
        }
        if(!"".equals(companyModel.getPhone())&&companyModel.getPhone()!=null){
            if (stringBuffer.length() > 0) {
                stringBuffer.append(",");
            }
            stringBuffer.append(" phone = '"+companyModel.getPhone()+"'");
        }
        if(!"".equals(companyModel.getMobile())&&companyModel.getMobile()!=null){
            if (stringBuffer.length() > 0) {
                stringBuffer.append(",");
            }
            stringBuffer.append(" mobile = '"+companyModel.getMobile()+"'");
        }

        if(!"".equals(companyModel.getMail())&&companyModel.getMail()!=null){
            if (stringBuffer.length() > 0) {
                stringBuffer.append(",");
            }
            stringBuffer.append(" mail = '"+companyModel.getMail()+"'");
        }
        if(!"".equals(companyModel.getFax())&&companyModel.getFax()!=null){
            if (stringBuffer.length() > 0) {
                stringBuffer.append(",");
            }
            stringBuffer.append(" fax = '"+companyModel.getFax()+"'");
        }
        if(!"".equals(companyModel.getPost())&&companyModel.getPost()!=null){
            if (stringBuffer.length() > 0) {
                stringBuffer.append(",");
            }
            stringBuffer.append(" post = '"+companyModel.getPost()+"'");
        }
        if(!"".equals(companyModel.getLocation())&&companyModel.getLocation()!=null){
            if (stringBuffer.length() > 0) {
                stringBuffer.append(",");
            }
            stringBuffer.append(" location = '"+companyModel.getLocation()+"'");
        }
        if(!"".equals(companyModel.getIntro())&&companyModel.getIntro()!=null){
            if (stringBuffer.length() > 0) {
                stringBuffer.append(",");
            }
            stringBuffer.append(" intro = '"+companyModel.getIntro()+"'");
        }
        if(stringBuffer.length()>0){
            stringBuffer.append(", update_time = now()");
        }
        if(stringBuffer.length()>0){
            stringBuffer.append(",");
        }
        stringBuffer.append(" remark='' where id = 1");
        sql= sql+stringBuffer.toString();
        return JDBC.update(sql);
    }
}
